package com.salesmanager.core.business.user.model;

import com.salesmanager.core.business.common.model.audit.AuditListener;
import com.salesmanager.core.business.common.model.audit.AuditSection;
import com.salesmanager.core.business.common.model.audit.Auditable;
import com.salesmanager.core.business.generic.model.SalesManagerEntity;
import org.hibernate.annotations.Cascade;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Entity
@EntityListeners(value = AuditListener.class)
@Table(name = "USER")
public class User extends SalesManagerEntity<Long, User> implements Auditable {


	private static final long serialVersionUID = 5401059537544058710L;

	@Id
	@Column(name = "USER_ID", unique = true, nullable = false)
	@TableGenerator(name = "TABLE_GEN", table = "SM_SEQUENCER", pkColumnName = "SEQ_NAME", valueColumnName = "SEQ_COUNT", pkColumnValue = "USER_SEQ_NEXT_VAL",
            initialValue = 100000, allocationSize = 1)
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "TABLE_GEN")
	private Long id;

	public User() {

	}

	public User(String userName, String password, String email) {

		this.adminName = userName;
		this.adminPassword = password;
		this.adminEmail = email;
	}

	@NotEmpty
	@Column(name = "ADMIN_NAME", length = 100, unique = true)
     private String adminName;
	@Column(name = "ADMIN_NICK")
	private String adminNick;
	@Column(name = "CHANNEL_NAME")
	private String channelName;
	@Column(name = "QQ_NUMBER")
	private String qqNumber;
	@Column(name = "REL_NAME")
	private String relName;
	@Column(name = "CITY")
	private String city;

	@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.REFRESH})
	@JoinTable(name = "USER_GROUP", joinColumns = {
			@JoinColumn(name = "USER_ID", nullable = false, updatable = false)}
			,
			inverseJoinColumns = {@JoinColumn(name = "GROUP_ID",
					nullable = false, updatable = false)}
	)
	@Cascade({
			org.hibernate.annotations.CascadeType.DETACH,
			org.hibernate.annotations.CascadeType.LOCK,
			org.hibernate.annotations.CascadeType.REFRESH,
			org.hibernate.annotations.CascadeType.REPLICATE

	})
	private List<Group> groups = new ArrayList<Group>();


	@Email
	@Column(name = "ADMIN_EMAIL")
	private String adminEmail;

	@NotEmpty
	@Column(name = "ADMIN_PASSWORD", length = 50)
	private String adminPassword;


	@Column(name = "ADMIN_FIRST_NAME")
	private String firstName;

	@Column(name = "ACTIVE")
	private boolean active = true;


	@Column(name = "ADMIN_LAST_NAME")
	private String lastName;

	@Column(name = "ADMIN_Q1")
	private String question1;

	@Column(name = "ADMIN_Q2")
	private String question2;

	@Column(name = "ADMIN_Q3")
	private String question3;

	@Column(name = "ADMIN_A1")
	private String answer1;

	@Column(name = "ADMIN_A2")
	private String answer2;

	@Column(name = "ADMIN_A3")
	private String answer3;

	@Embedded
	private AuditSection auditSection = new AuditSection();

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "LAST_ACCESS")
	private Date lastAccess;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "LOGIN_ACCESS")
	private Date loginTime;


	/**
	 * Add by Liutong
	 * 用户类型 1：系统管理员  2：卖家
	 */
	@Column(name = "USER_TYPE", length = 1)
	private int userType = 1;

	@Column(name = "STATUS", length = 1)
	private int status = 1;
	@Column(name = "TELPHONE")
	private String telPhone;
	//登陆错误次数 成功后归零
	@Column(name = "ERROR_TIMES")
	private Integer errorTimes=0;
	//是否锁定
	@Column(name = "IS_LOCK")
	private Integer isLock=0;
	   @Transient
		private String phoneCode ;

	@Override
	public Long getId() {
		return this.id;
	}

	@Override
	public void setId(Long id) {
		this.id = id;
	}

	@Override
	public AuditSection getAuditSection() {
		return auditSection;
	}

	@Override
	public void setAuditSection(AuditSection audit) {
		auditSection = audit;

	}



	public String getAdminEmail() {
		return adminEmail;
	}

	public void setAdminEmail(String adminEmail) {
		this.adminEmail = adminEmail;
	}

	public String getAdminPassword() {
		return adminPassword;
	}

	public void setAdminPassword(String adminPassword) {
		this.adminPassword = adminPassword;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getQuestion1() {
		return question1;
	}

	public void setQuestion1(String question1) {
		this.question1 = question1;
	}

	public String getQuestion2() {
		return question2;
	}

	public void setQuestion2(String question2) {
		this.question2 = question2;
	}

	public String getQuestion3() {
		return question3;
	}

	public void setQuestion3(String question3) {
		this.question3 = question3;
	}

	public String getAnswer1() {
		return answer1;
	}

	public void setAnswer1(String answer1) {
		this.answer1 = answer1;
	}

	public String getAnswer2() {
		return answer2;
	}

	public void setAnswer2(String answer2) {
		this.answer2 = answer2;
	}

	public String getAnswer3() {
		return answer3;
	}

	public void setAnswer3(String answer3) {
		this.answer3 = answer3;
	}

	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}

	public List<Group> getGroups() {
		return groups;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public boolean isActive() {
		return active;
	}

	public void setLastAccess(Date lastAccess) {
		this.lastAccess = lastAccess;
	}

	public Date getLastAccess() {
		return lastAccess;
	}

	public void setLoginTime(Date loginTime) {
		this.loginTime = loginTime;
	}

	public Date getLoginTime() {
		return loginTime;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getUserType() {
		return userType;
	}
	public void setUserType(int userType) {
		this.userType = userType;
	}
	public void setTelPhone(String telPhone) {
		this.telPhone = telPhone;
	}
	public String getTelPhone( ) {
		return telPhone;
	}
	public void setErrorTimes(Integer errorTimes) {
		this.errorTimes = errorTimes;
	}
	public Integer getIsLock( ) {
		return isLock;
	}
	public void setIsLock(Integer isLock) {
		this.isLock = isLock;
	}
	public Integer getErrorTimes( ) {
		return errorTimes;
	}
	@Transient
	public String getPhoneCode() {
		return phoneCode;
	}

	public void setPhoneCode(String phoneCode) {
		this.phoneCode = phoneCode;
	}

	public String getAdminName() {
		return adminName;
	}

	public void setAdminName(String adminName) {
		this.adminName = adminName;
	}

	public String getAdminNick() {
		return adminNick;
	}

	public void setAdminNick(String adminNick) {
		this.adminNick = adminNick;
	}

	public String getQqNumber() {
		return qqNumber;
	}

	public void setQqNumber(String qqNumber) {
		this.qqNumber = qqNumber;
	}

	public String getRelName() {
		return relName;
	}

	public void setRelName(String relName) {
		this.relName = relName;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

    public String getChannelName() {
        return channelName;
    }

    public void setChannelName(String channelName) {
        this.channelName = channelName;
    }
}